﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TVVLIEF
    {

        public TVVLIEF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVVLIEF();
            MESSAGES += "WB2KO;";
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, System.EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                VVIS = "00000000";
                SART = "";;
                VSPED ="";
                KEY = "";
            }
            else
            {
                VVIS = TMV.VVAVISNR;
                SART = ((VVVLIEF)MV).VVVERSART;
                VSPED = ((VVVLIEF)MV).VVSPED;
                KEY = ((VVVLIEF)MV).VVLIEFSCH;
                
                
            } 
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VVVLIEF)MV).UpdateVVLIEF(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 8)
                return;
            dwData = TBMain.NewRow();
            dwData["VVMANDANT"] = app.Mandant;
            dwData["VVLIEFSCH"] = ORDERNUM;
            SResult = ((VVVLIEF)MV).QueryVVLIEF(dwData, 1);
            MergeData(SResult, true);
        }
        public override void OnVKADRChg(string vkp)
        {
            base.OnVKADRChg(vkp);
        }
        public override void OnVVTChg(string vvt)
        {
            if (vvt.Length < 20)
                return;
            ORDERNUM = vvt.Substring(12, 8);
            base.OnVVTChg(vvt);
        }
        public override void OnVVISChg(string vvis)
        {
            if (vvis.Length < 8)
                return;
            if (vvis.Trim() == "00000000") return;
            TMV.VVAVISNR = vvis;
            base.OnVVTChg(vvis);
        }
        private void VKK4VVSARTCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (dwVKK == null)
                return;
            SART = dwVKK["VKKVERSART"].ToString();
            VSPED = dwVKK["VKKSPED"].ToString();
         }

        private void VK4VVCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (dwVKVV == null)
                return;
           TMV.VVANAME1= dwVKVV["VKALNAME1"].ToString();
           TMV.VVANAME2= dwVKVV["VKALNAME2"].ToString();
           TMV.VVASTRASSE= dwVKVV["VKALSTRASSE1"].ToString();
           TMV.VVASTRASSE2= dwVKVV["VKALSTRASSE2"].ToString();
           TMV.VVLAND= dwVKVV["VKALLKZ"].ToString();
           TMV.VVPLZ= dwVKVV["VKALPLZ"].ToString();
           TMV.VVORT = dwVKVV["VKALORT"].ToString();
           TMV.VVAORT = dwVKVV["VKALORT"].ToString();
           TMV.VVABZBEZ = dwVKVV["VKALBZBEZ"].ToString();
           TMV.VVKUNDNRV= dwVKVV["VKAKUNDNR1"].ToString();
           TMV.VVADRSLV= dwVKVV["VKAADRSLK11"].ToString();
         }

        private void KD4VVCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (dwKDR == null)
                return;
           TMV.VVANAME1= dwKDR["KDANAME1"].ToString();
           TMV.VVANAME2= dwKDR["KDANAME2"].ToString();
           TMV.VVASTRASSE= dwKDR["KDASTRASSE1"].ToString();
           TMV.VVASTRASSE2= dwKDR["KDASTRASSE2"].ToString();
           TMV.VVLAND= dwKDR["KDALAND"].ToString();
           TMV.VVPLZ= dwKDR["KDAPLZ"].ToString();
           TMV.VVORT= dwKDR["KDAORT"].ToString();
           TMV.VVKUNDNRV= dwKDR["KDAKUNDNR"].ToString();
           TMV.VVADRSLV= dwKDR["KDAADRSL"].ToString();           
           TMV.VVAORT = (dwKDR["KDATTEX"].ToString().Trim()+":"+dwKDR["KDATEL"].ToString().Trim()).PadRight(40);
           TMV.VVABZBEZ = dwKDR["KDAPOSTFACH"].ToString().Substring(0,13);
         }
    }
}